package com.jzo2o.customer.controller.consumer;

import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.jni.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "用户端 - 用户地址相关接口")
public class AddressBookController {

    @Autowired
    private IAddressBookService addressBookService;

    /**
     * 查询用户默认地址
     * @return
     */
    @GetMapping("/defaultAddress")
    @ApiOperation("查询用户默认地址值")
    public AddressBookResDTO findDefaultAddress(){
        return addressBookService.findDefaultAddress();
    }

    /**
     * 新增地址
     */
    @PostMapping
    @ApiOperation("新增地址")
    public void saveAddress(@RequestBody AddressBookUpsertReqDTO addressBookUpsertReqDTO){
      addressBookService.saveAddress(addressBookUpsertReqDTO);
    }

    /**
     * 查询地址列表
     * @param addressBookUpsertReqDTO
     */
    @GetMapping ("/page")
    @ApiOperation("查询地址列表")
    public PageResult<AddressBookResDTO> page(AddressBookPageQueryReqDTO addressBookUpsertReqDTO){
        return addressBookService.findByPage(addressBookUpsertReqDTO);
    }

    /**
     * 查询地址详情
     * @param id
     * @return
     */
    @GetMapping ("/{id}")
    @ApiOperation("查询地址详情")
    public AddressBookResDTO findById(@PathVariable Long id){
        return addressBookService.findById(id);
    }

    /**
     * 修改地址
     * @param id
     * @param addressBookUpsertReqDTO
     */
    @PutMapping("/{id}")
    @ApiOperation("修改地址")
    public void updateAddressBook(@PathVariable Long id,
            @RequestBody AddressBookUpsertReqDTO addressBookUpsertReqDTO){
        addressBookService.updateAddressBook(id,addressBookUpsertReqDTO);
    }

    /**
     * 删除地址
     * @param ids
     */
    @DeleteMapping ("/batch")
    @ApiOperation("删除地址")
    public void delete(@RequestBody List<Long> ids){
            addressBookService.removeByIds(ids);
    }


    /**
     * 修改是否为默认地址
     * @param id
     * @param flag
     */
    @PutMapping("/default")
    @ApiOperation("修改是否为默认地址")
    public void updateDefaultStatus(Long id,Integer flag){
        addressBookService.updateDefaultStatus(id,flag);
    }

}